<?php

class vtnsCultureInfo extends sfCultureInfo
{
    const DEFAULT_CULTURE = 'en_US';

    static private $defaultCultures = array('en','fr', 'es', 'it', 'nl', 'ja', 'zh');

    /**
     * Replace some cultures to prevent PHP breakage: setting LC_CTYPE to
     * these locales breaks functions with an 'I' in their name,
     * but using alternative locales seems to work properly (bug in
     * zend_tolower's usage of tolower).
     *
     * See FS#11840, FS#12562 and http://bugs.php.net/18556
     */
    static private $replacementCultures = array(
         'az_AZ'      => 'az',
         'tr_TR'      => 'tr',
         'zh_Hans_HK' => 'zh_Hans',
         'ha_Latn_NG' => 'ha_Latn',
         'fo'         => 'fo_FO',
         'hi'         => 'hi_IN');

    static public function getCultureFromLocale($locale)
    {
        if (empty($locale)) {
            return self::DEFAULT_CULTURE;
        }
        if (parent::validCulture($locale)) {
            $culture = $locale;
            $cultureValidityChecked = true;
        } else {
            $culture = substr($locale, 0, 2);
            $cultureValidityChecked = false;
        }
        if (array_key_exists($culture, self::$replacementCultures)) {
            $culture = self::$replacementCultures[$culture];
            $cultureValidityChecked = false;
        }
        if (!$cultureValidityChecked && !parent::validCulture($culture)) {
            $culture = self::DEFAULT_CULTURE;
        }
        if (!in_array(substr($locale, 0, 2), self::$defaultCultures))
            $culture = self::DEFAULT_CULTURE;

        return $culture;
    }
}

?>
